import subprocess#用于直接执行adb命令
import tkinter as tk
from tkinter import Canvas,Entry
from PIL import Image,ImageTk
from time import sleep
# 截图
def screenshot(save_path):
    result=subprocess.run("adb devices",shell=True,text=True,capture_output=True)
    if "device\n" not in result.stdout:
        print("请连接手机")
        return result.stdout
    shot_path_device = "/sdcard/screenshot.png"
    subprocess.run(f"adb shell screencap -p {shot_path_device}",shell=True)#截图
    subprocess.run(f"adb pull {shot_path_device} {save_path}",shell=True)#
    subprocess.run(f"adb shell rm {shot_path_device}",shell=True)
    print("截图完成")
    return "device\n"

class immark:
    def __init__(self,window,image_path):
        self.window=window
        self.canvas=tk.Canvas(window,width=1200,height=800)
        self.canvas.pack()

        self.image=Image.open(image_path)
        self.image_tk=ImageTk.PhotoImage(self.image)

        c_x = self.image.width / 2
        c_y = self.image.height / 1.5

        self.canvas.create_image(c_x,c_y,anchor=tk.S,image=self.image_tk)




        self.points=[]
        self.point_id=[]

        self.canvas.bind("<Button-1>",self.add_point)

        self.distance_lab=tk.Label(window,text="距离:")
        self.distance_lab.pack()


        self.distance=0

        self.n_input=Entry(window)
        self.n_input.insert(0,"1.15")
        self.n_input.pack()

        self.scan=screenshot("screenshot.png")

    def update_image(self):
        self.image=Image.open("screenshot.png")
        self.image_tk=ImageTk.PhotoImage(self.image)
        c_x=self.image.width/2
        c_y=self.image.height/1.5
        self.canvas.create_image(c_x,c_y,anchor=tk.S,image=self.image_tk)


    def add_point(self,event):
        x,y=event.x,event.y
        if len(self.points)<2:
            self.points.append((x,y))
            self.point_id.append(self.canvas.create_oval(x-5,y-5,x+5,y+5,fill="red"))
        else:
            self.points.pop(len(self.points)-2)
            self.canvas.delete(self.point_id[len(self.point_id)-2])

            self.points.append((x,y))
            self.point_id.append(self.canvas.create_oval(x-5,y-5,x+5,y+5,fill="red"))
    def click_button(self):
        if len(self.points)<2:
            self.distance_lab.config(text="请选择两个点")
        else:
            distance=self.get_distance(self.points[0],self.points[1])
            self.distance_lab.config(text="距离为:{}".format(distance))
            self.jump(float(self.n_input.get()),self.distance)
            sleep(0.8)
            screenshot("screenshot.png")
            if "device\n" not in self.scan:
                app.distance_lab.config(text="请链接手机adb")
            self.update_image()
            self.points=[]
            for i in self.point_id:
                self.canvas.delete(i)
            self.point_id=[]





    def get_distance(self,point1,point2):
        x1,y1=point1
        x2,y2=point2
        self.distance=((x2-x1)**2+(y2-y1)**2)**0.5
        return self.distance

    def jump(self,n,distance):
        jdistance=int(self.distance*n)
        subprocess.run(f"adb shell input swipe 500 500 500 500 {jdistance}", shell=True)

emp_image=Image.new("RGB",(300,300),"white")
emp_image.save("screenshot.png")
scan=screenshot("screenshot.png")
window=tk.Tk()
app=immark(window,"screenshot.png")
if "device\n" not in scan:
    app.distance_lab.config(text="请链接手机adb")

b=tk.Button(window,text="跳",command=app.click_button)
b.pack()

window.mainloop()



